package cn.ccenjie.wsy.social.alipay.api;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayUserInfoShareRequest;
import com.alipay.api.response.AlipayUserInfoShareResponse;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.social.oauth2.AbstractOAuth2ApiBinding;

public class AlipayImpl extends AbstractOAuth2ApiBinding implements Alipay {

    private static final String INFO_URL = "https://openapi.alipay.com/gateway.do";

    private String appid;

    private String authToken;

    public AlipayImpl(String appid, String authToken) {
        this.appid = appid;
        this.authToken = authToken;
    }

    @Override
    public AlipayInfo info() {
        AlipayClient client = new DefaultAlipayClient(INFO_URL, appid, "");
        AlipayUserInfoShareRequest request = new AlipayUserInfoShareRequest();
        AlipayInfo ret = new AlipayInfo();
        try {
            AlipayUserInfoShareResponse alipayUserInfoShareResponse = client.certificateExecute(request);
            BeanCopier.create(AlipayUserInfoShareResponse.class, AlipayInfo.class, false).copy(alipayUserInfoShareResponse, ret, null);
        } catch (AlipayApiException e) {
            throw new RuntimeException("请求支付宝用户信息出现错误", e);
        }
        return ret;
    }
}
